<?php
include("functions.php.inc");
$command ="";
if (isset($_GET['command'])) {
	if (trim($_GET['command']) != "") {
		$command = $_GET['command'];
	}
}


header ("Content-Type:text/xml");

if ($command=="version") {
$xml = <<< EOL
<?xml version="1.0" encoding="ISO-8859-1"?>
<beerwatcher>
	<result>
		<command>$command</command>
		<output>0.0.1</output>
		<status>Success</status>
	</result>
</beerwatcher>
EOL;
echo $xml;
exit;
}else if ($command=="getProbes") {
$probes=getLinks();
$ps = "";
if ($probes > 0) {
	for ($i = 0; $i<$probes;$i++) {
		$c = $i;
		$c++;
		$name = getName($c);
		$ps .= "<probe>\n";
		$ps .= "\t<name>$name</name>\n";
		$ps .= "\t<number>$c</number>\n";
		$ps .= "</probe>\n";
		
	}
}
$xml = <<< EOL
<?xml version="1.0" encoding="ISO-8859-1"?>
<beerwatcher>
	<result>
		<status>Success</status>
		<command>$command</command>
		$ps
	</result>
</beerwatcher>
EOL;
echo $xml;
exit;
} else if  ($command=="getCurrentTemp") {
if (!isset($_GET['probe'])) {
	exit;
}
if (trim($_GET['probe']) == "") {
	exit;
}
$probe = $_GET['probe'];
$temp=getTemp($probe);
$xml = <<< EOL
<?xml version="1.0" encoding="ISO-8859-1"?>
<beerwatcher>
	<result>
		<status>Success</status>
		<command>$command</command>
		<temp>$temp</temp>
	</result>
</beerwatcher>
EOL;
echo $xml;
exit;
} else if ($command == "getOG") {
if (!isset($_GET['probe'])) {
	exit;
}
if (trim($_GET['probe']) == "") {
	exit;
}
$probe = $_GET['probe'];
$og=getOg($probe);
$xml = <<< EOL
<?xml version="1.0" encoding="ISO-8859-1"?>
<beerwatcher>
	<result>
		<status>Success</status>
		<command>$command</command>
		<og>$og</og>
	</result>
</beerwatcher>
EOL;
echo $xml;

} else if  ($command=="getHeatOn") {
if (!isset($_GET['probe'])) {
	exit;
}
if (trim($_GET['probe']) == "") {
	exit;
}
$probe = $_GET['probe'];
$temp=heatOn($probe);
$xml = <<< EOL
<?xml version="1.0" encoding="ISO-8859-1"?>
<beerwatcher>
	<result>
		<status>Success</status>
		<command>$command</command>
		<on>$temp</on>
	</result>
</beerwatcher>
EOL;
echo $xml;
exit;

} else if  ($command=="getSetTemp") {
if (!isset($_GET['probe'])) {
        exit;
}
if (trim($_GET['probe']) == "") {
        exit;
}
$probe = $_GET['probe'];
$tt=targetTemp($probe);
$xml = <<< EOL
<?xml version="1.0" encoding="ISO-8859-1"?>
<beerwatcher>
        <result>
                <status>Success</status>                                      
                <command>$command</command>
                <temp>$tt</temp>
        </result>
</beerwatcher>
EOL;
echo $xml;
exit;
}  else if  ($command=="getTempVariation") {
if (!isset($_GET['probe'])) {
        exit;
}
if (trim($_GET['probe']) == "") {
        exit;
}
$probe = $_GET['probe'];
$va=variance($probe);
$xml = <<< EOL
<?xml version="1.0" encoding="ISO-8859-1"?>
<beerwatcher>
        <result>
                <status>Success</status>
                <command>$command</command>
                <variance>$va</variance>
        </result>
</beerwatcher>
EOL;
echo $xml;
exit;
} else if  ($command=="getName") {
if (!isset($_GET['probe'])) {
        exit;
}
if (trim($_GET['probe']) == "") {
        exit;
}
$probe = $_GET['probe'];
$gn=getName($probe);
$xml = <<< EOL
<?xml version="1.0" encoding="ISO-8859-1"?>
<beerwatcher>
        <result>
                <status>Success</status>
                <command>$command</command>
                <name>$gn</name>
        </result>
</beerwatcher>
EOL;
echo $xml;
exit;
} else if  ($command=="saveChanges") {
if (!isset($_GET['probe'])) {
        exit;
}
if (trim($_GET['probe']) == "") {
        exit;
}

if (!isset($_GET['temp'])) {
        exit;
}
if (trim($_GET['temp']) == "") {
        exit;
}

if (!isset($_GET['variation'])) {
        exit;
}
if (trim($_GET['variation']) == "") {
        exit;
}

if (!isset($_GET['name'])) {
        exit;
}
if (trim($_GET['name']) == "") {
        exit;
}
$probe = $_GET['probe'];
$og = "0.00";
if (isset($_GET['og'])) {
	if ($_GET['og'] != "") {
		$og = $_GET['og'];
	}
	setOG($probe,$og);
}


$name = $_GET['name'];
$variation = $_GET['variation'];
$temp = $_GET['temp'];
setName($probe,$name);
setVariance($probe,$variation);
setTargetTemp($probe,$temp);
$xml = <<< EOL
<?xml version="1.0" encoding="ISO-8859-1"?>
<beerwatcher>
        <result>
                <status>Success</status>
                <command>$command</command>
        </result>
</beerwatcher>
EOL;
echo $xml;
exit;
} else if  ($command=="makeLabel") {
$date = "";
$name = "";
$og = "";
$fg = "";
$query = "";
$copies = "";
if (isset($_GET['date'])) {
	if ($_GET['date'] != "") {
		$date = $_GET['date'];
		$date = urlencode($date);
	}
}
if (isset($_GET['name'])) {
	if ($_GET['name'] != "") {
		$name = $_GET['name'];
		$name = urlencode($name);
	}
}
if (isset($_GET['og'])) {
	if ($_GET['og'] != "") {
		$og = $_GET['og'];
		$og = urlencode($og);
	}
}
if (isset($_GET['fg'])) {
	if ($_GET['fg'] != "") {
		$fg = $_GET['fg'];
		$fg = urlencode($fg);
	}
}
if ($date == "") {
	$date = date("Y-m-d");
}

if ($date != "") {
	if ($query != "") {
		$query .="&";
	}
	$query .= "date=$date";
}
if ($name != "") {
	if ($query != "") {
		$query .="&";
	}
	$query .= "name=$name";
}
if ($og != "") {
	if ($query != "") {
		$query .="&";
	}
	$query .= "og=$og";
}
if ($fg != "") {
	if ($query != "") {
		$query .="&";
	}
	$query .= "fg=$fg";
}
if ($copies == "") {
	$copies = 1;
}
if ($copies != "") {
	if ($query != "") {
		$query .="&";
	}
	$query .= "copies=$copies";
}


$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/make.php?$query"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec($ch); 
curl_close($ch); 

$xml = <<< EOL
<?xml version="1.0" encoding="ISO-8859-1"?>
<beerwatcher>
	<result>
		<status>Success</status>
		<command>$command</command>
		<message>Job Sent to Printer</message>
	</result>
</beerwatcher>
EOL;
echo $xml;
exit;
}

?>
